<?php
/**
 * Created by JetBrains PhpStorm.
 * File: UserAction.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-10
 * Time: 上午2:07
 */

	class UserAction extends CommonAction{

		//用户列表
		public function index(){

			$user = D('UserView');
			$role = M('Role');
			import("ORG.Util.page");

			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['username'] = array("like","%{$_POST['key']}%");
							break;
						case 3 :
							$datas['email'] = array('like',"%{$_POST['key']}%");
							break;
					}
					if(isset($_GET['rid']) && !empty($_GET['rid'])){
						$datas['role_id'] = $_GET['rid'];
					}
					$list = $user->where($datas)->order('id desc')->select();
					$count = $user->where($datas)->count();
					if($count<1){
					$this->error('没有该用户数据');
					//	$this->ajaxReturn(0,"没有该用户数据！",0);
					}elseif($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));

					$show = $page->fpage($pagedata);
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');
				}

			}else{
				if(isset($_GET['rid']) && !empty($_GET['rid'])){
					$count = $user->where('role_id='.$_GET['rid'])->count();
					if($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);
					$list = $user->where('role_id='.$_GET['rid'])->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
				}else{
					$count = $user->count();
					if($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);
					$list = $user->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
				}
				if($count==0){
					$this->assign('isshow', 'no');
					$this->assign('nobody', 'no');
				}else{
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');
				}
			}
			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display('index');
		}

		//指向添加用户界面
		function add(){
		//	setcookie("load", MODULE_NAME, time()+50);
			$role = M('Role');
			$list = $role->where('status=1')->select();
			if(empty($list)){
				$this->error('目前没有用户分组,请先创建用户分组');
			}else{
				$this->assign('list', $list);
				$this->display('add');
			}
		}

		//添加用户操作
		function insert(){
		//	$this->assign('waitSecond',3);
			if(!empty($_POST['role_id'])){
				$user = D('User');
				if($list = $user->create()){
					if($user_id=$user->add()){
						$role = M('RoleUser');
						$data['user_id'] = $user_id;
						$data['role_id'] = $_POST['role_id'];
						if($role->add($data)){
							$this->success('添加用户成功', __APP__.'/User/index');
						}else{
							$this->error('添加到用户组列表失败');
						}
					}else{
						$this->error('添加用户失败');
					}
				}else{
					$this->error($user->getError());
				}
			}else{
				$this->error('请先创建用户分组');
			}
		}

		//删除用户操作
		function del(){
			if(!empty($_POST['del'])){
				$user = D('User');
					foreach($_POST['del'] as $key=>$value){
						$num = $user->relation(true)->delete($value);
					}
					if($num>0){
						$this->success('删除用户成功');
					}else{
						$this->error('删除用户失败');
					}
			}else{
				$this->error('请选用用户');
			}
		}

		//指向修改用户界面
		function edit(){
			if(isset($_GET['id']) && !empty($_GET['id'])){
				$user = D('User');
				$list = $user->relation(true)->find($_GET['id']);
				$role = M('Role');
				$group = $role->select();
				$this->assign('group', $group);
				$this->assign('list', $list);
				$this->display();
			}else{
				$this->error('异常操作');
			}

		}

		//修改用户操作
		function update(){
			if(isset($_POST['id']) && !empty($_POST['id'])){
				$user = D('User');
				if($list = $user->create()){
					if(empty($list['password'])){
						unset($list['password']);
						$list['roleuser']['role_id'] = $_POST['role_id'];
							if($user->relation(true)->save($list)){
								$this->success('修改用户成功', __APP__.'/User/index');
							}else{
								$this->error('修改用户失败');
							}
					}else{
						$list['roleuser']['role_id'] = $_POST['role_id'];
						$list['password'] = md5($list['password']);
						if($user->relation(true)->save($list)){
							$this->success('修改用户成功',  __APP__.'/User/index');
						}else{
							$this->error('修改用户失败');
						}
					}
				}else{
					$this->error($user->getError());
				}
			}else{
				$this->error('异常操作');
			}
		}


}